1
|
|
|
/** global: wp, pollux, CodeMirror */ |
2
|
|
|
|
3
|
|
|
pollux.dependency = {}; |
4
|
|
|
pollux.editors = {}; |
5
|
|
|
pollux.featured = {}; |
6
|
|
|
pollux.metabox = {}; |
7
|
|
|
pollux.tabs = {}; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @return bool |
11
|
|
|
*/ |
12
|
|
|
pollux.classListAction = function( bool ) |
13
|
|
|
{ |
14
|
|
|
return bool ? 'add' : 'remove'; |
15
|
|
|
}; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
pollux.dependency.ajax = function( action, el, type ) |
21
|
|
|
{ |
22
|
|
|
var args = pollux.dependency.getAjaxOptions( el ); |
23
|
|
|
wp.ajax.send({ |
24
|
|
|
error: args.error, |
25
|
|
|
success: args.success, |
26
|
|
|
data: { |
27
|
|
|
_ajax_nonce: wp.updates.ajaxNonce, |
28
|
|
|
action: action, |
29
|
|
|
plugin: args.plugin, |
30
|
|
|
type: type, |
31
|
|
|
} |
32
|
|
|
}); |
33
|
|
|
}; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return object |
37
|
|
|
*/ |
38
|
|
|
pollux.dependency.getAjaxOptions = function( el ) |
39
|
|
|
{ |
40
|
|
|
return { |
41
|
|
|
error: pollux.dependency.onError.bind( el ), |
42
|
|
|
plugin: el.getAttribute( 'data-plugin' ), |
43
|
|
|
slug: el.getAttribute( 'data-slug' ), |
44
|
|
|
success: pollux.dependency.onSuccess.bind( el ), |
45
|
|
|
}; |
46
|
|
|
}; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
pollux.dependency.init = function() |
52
|
|
|
{ |
53
|
|
|
pollux.dependency.buttons = document.querySelectorAll( '.pollux-notice a.button' ); |
54
|
|
|
[].forEach.call( pollux.dependency.buttons, function( button ) { |
55
|
|
|
button.addEventListener( 'click', pollux.dependency.onClick ); |
56
|
|
|
}); |
57
|
|
|
}; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
pollux.dependency.install = function( el, args ) |
63
|
|
|
{ |
64
|
|
|
pollux.dependency.updateButtonText( el, 'pluginInstallingLabel' ); |
65
|
|
|
el.classList.add( 'updating-message' ); |
66
|
|
|
return wp.updates.ajax( 'install-plugin', args ); |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
pollux.dependency.onClick = function( ev ) |
73
|
|
|
{ |
74
|
|
|
var action = this.href.match(/action=([^&]+)/); |
75
|
|
|
if( action === null )return; |
|
|
|
|
76
|
|
|
action = action[1].split('-')[0]; |
77
|
|
|
if( !pollux.dependency[action] )return; |
|
|
|
|
78
|
|
|
this.blur(); |
79
|
|
|
ev.preventDefault(); |
80
|
|
|
if( this.classList.contains( 'updating-message' ))return; |
|
|
|
|
81
|
|
|
pollux.dependency[action]( this, pollux.dependency.getAjaxOptions( this )); |
82
|
|
|
}; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
pollux.dependency.onError = function( response ) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
window.location = this.href; |
90
|
|
|
}; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
pollux.dependency.onSuccess = function( response ) |
96
|
|
|
{ |
97
|
|
|
var el = this; |
98
|
|
|
var type = response.install ? 'install' : 'update'; |
99
|
|
|
if( !response.activate_url ) { |
100
|
|
|
return pollux.dependency.ajax( 'pollux/dependency/activate_url', el, type ); |
101
|
|
|
} |
102
|
|
|
pollux.dependency.setUpdatedMessage( el, type ); |
103
|
|
|
if( response.activate_url ) { |
|
|
|
|
104
|
|
|
setTimeout( function() { |
105
|
|
|
pollux.dependency.setActivateButton( el, response ); |
106
|
|
|
}, 1000 ); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
}; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
pollux.dependency.setActivateButton = function( el, response ) |
114
|
|
|
{ |
115
|
|
|
el.classList.remove( 'updated-message' ); |
116
|
|
|
el.classList.remove( 'button-disabled' ); |
117
|
|
|
el.classList.add( 'button-primary' ); |
118
|
|
|
el.href = response.activate_url; |
119
|
|
|
pollux.dependency.updateButtonText( el, 'activatePluginLabel' ); |
120
|
|
|
}; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
pollux.dependency.setUpdatedMessage = function( el, type ) |
126
|
|
|
{ |
127
|
|
|
el.classList.remove( 'updating-message' ); |
128
|
|
|
el.classList.add( 'updated-message' ); |
129
|
|
|
el.classList.add( 'button-disabled' ); |
130
|
|
|
pollux.dependency.updateButtonText( el, ( |
131
|
|
|
type === 'install' ? 'pluginInstalledLabel' : 'updatedLabel' |
132
|
|
|
)); |
133
|
|
|
}; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
pollux.dependency.updateButtonText = function( el, l10nkey ) |
139
|
|
|
{ |
140
|
|
|
if( !wp.updates.l10n[l10nkey] )return; |
|
|
|
|
141
|
|
|
var label = wp.updates.l10n[l10nkey].replace( '%s', el.getAttribute( 'data-name' )); |
142
|
|
|
if( el.innerHTML !== label ) { |
143
|
|
|
el.innerHTML = label; |
144
|
|
|
} |
145
|
|
|
}; |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
pollux.dependency.upgrade = function( el, args ) |
151
|
|
|
{ |
152
|
|
|
pollux.dependency.updateButtonText( el, 'updatingLabel' ); |
153
|
|
|
el.classList.add( 'updating-message' ); |
154
|
|
|
return wp.updates.ajax( 'update-plugin', args ); |
155
|
|
|
}; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
pollux.editors.disable = function( index ) |
161
|
|
|
{ |
162
|
|
|
pollux.editors.all[index].setOption( 'theme', 'disabled' ); |
163
|
|
|
pollux.editors.all[index].setOption( 'readOnly', 'nocursor' ); |
164
|
|
|
}; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
pollux.editors.enable = function( index ) |
170
|
|
|
{ |
171
|
|
|
pollux.editors.all[index].setOption( 'theme', 'pollux' ); |
172
|
|
|
pollux.editors.all[index].setOption( 'readOnly', false ); |
173
|
|
|
}; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
pollux.editors.init = function() |
179
|
|
|
{ |
180
|
|
|
pollux.editors.all = []; |
181
|
|
|
[].forEach.call( document.querySelectorAll( '.pollux-code' ), function( editor, index ) { |
182
|
|
|
pollux.editors.all[index] = CodeMirror.fromTextArea( editor, { |
183
|
|
|
gutters: ['CodeMirror-lint-markers'], |
184
|
|
|
highlightSelectionMatches: { wordsOnly: true }, |
185
|
|
|
lineNumbers: true, |
186
|
|
|
lint: true, |
187
|
|
|
mode: 'text/yaml', |
188
|
|
|
showInvisibles: true, |
189
|
|
|
showTrailingSpace: true, |
190
|
|
|
styleActiveLine: true, |
191
|
|
|
tabSize: 2, |
192
|
|
|
theme: 'pollux', |
193
|
|
|
viewportMargin: Infinity, |
194
|
|
|
}); |
195
|
|
|
pollux.editors.all[index].setOption( 'extraKeys', { |
196
|
|
|
Tab: function( cm ) { |
197
|
|
|
var spaces = Array( cm.getOption( 'indentUnit' ) + 1 ).join( ' ' ); |
198
|
|
|
cm.replaceSelection( spaces ); |
199
|
|
|
}, |
200
|
|
|
}); |
201
|
|
|
pollux.editors.all[index].display.wrapper.setAttribute( 'data-disabled', editor.getAttribute( 'data-disabled' )); |
202
|
|
|
if( editor.readOnly ) { |
203
|
|
|
pollux.editors.disable( index ); |
204
|
|
|
} |
205
|
|
|
}); |
206
|
|
|
}; |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
pollux.featured.init = function() |
212
|
|
|
{ |
213
|
|
|
jQuery( '#postimagediv' ) |
214
|
|
|
.on( 'click', '#pollux-set-featured', function( ev ) { |
215
|
|
|
ev.preventDefault(); |
216
|
|
|
wp.media.view.settings.post.featuredImageId = Math.round( jQuery( '#featured' ).val() ); |
217
|
|
|
pollux.featured.frame = wp.media.featuredImage.frame; |
218
|
|
|
pollux.featured.frame().open(); |
219
|
|
|
}) |
220
|
|
|
.on( 'click', '#pollux-remove-featured', function( ev ) { |
221
|
|
|
ev.preventDefault(); |
222
|
|
|
pollux.featured.set(-1); |
223
|
|
|
}); |
224
|
|
|
}; |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
|
pollux.featured.select = function() |
230
|
|
|
{ |
231
|
|
|
if( !wp.media.view.settings.post.featuredImageId )return; |
232
|
|
|
var selection = this.get( 'selection' ).single(); |
233
|
|
|
pollux.featured.set( selection ? selection.id : -1 ); |
234
|
|
|
}; |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return void |
238
|
|
|
*/ |
239
|
|
|
pollux.featured.set = function( id ) |
240
|
|
|
{ |
241
|
|
|
wp.media.view.settings.post.featuredImageId = Math.round( id ); |
242
|
|
|
wp.media.post( 'pollux/archives/featured/html', { |
243
|
|
|
_wpnonce: document.querySelector( '#_wpnonce' ).value, |
244
|
|
|
post_type: document.querySelector( '#archive-type' ).value, |
245
|
|
|
thumbnail_id: id, |
246
|
|
|
}).done( function( html ) { |
247
|
|
|
document.querySelector( '#postimagediv > .inside' ).innerHTML = html; |
248
|
|
|
}); |
249
|
|
|
}; |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
|
|
pollux.metabox.hasValue = function( el ) |
255
|
|
|
{ |
256
|
|
|
if( el.type === 'checkbox' ) { |
257
|
|
|
return el.checked === true; |
258
|
|
|
} |
259
|
|
|
return el.value !== ''; |
260
|
|
|
}; |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return void |
264
|
|
|
*/ |
265
|
|
|
pollux.metabox.init = function() |
266
|
|
|
{ |
267
|
|
|
var depends = document.querySelectorAll( '.rwmb-input [data-depends]' ); |
268
|
|
|
[].forEach.call( depends, function( el ) { |
269
|
|
|
var dependency = pollux.metabox.setVisibility( el ); |
270
|
|
|
var event = dependency.type === 'checkbox' ? 'change' : 'keyup'; |
271
|
|
|
dependency.addEventListener( event, function() { |
272
|
|
|
pollux.metabox.setVisibility( el ); |
273
|
|
|
}); |
274
|
|
|
}); |
275
|
|
|
}; |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return element |
279
|
|
|
*/ |
280
|
|
|
pollux.metabox.setVisibility = function( el ) |
281
|
|
|
{ |
282
|
|
|
var dependency = document.getElementById( el.getAttribute( 'data-depends' )); |
283
|
|
|
var action = pollux.classListAction( !pollux.metabox.hasValue( dependency )); |
284
|
|
|
el.closest( '.rwmb-field' ).classList[action]( 'hidden' ); |
285
|
|
|
return dependency; |
286
|
|
|
}; |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return void |
290
|
|
|
*/ |
291
|
|
|
pollux.tabs.init = function() |
292
|
|
|
{ |
293
|
|
|
pollux.tabs.active = document.querySelector( '#pollux-active-tab' ); |
294
|
|
|
pollux.tabs.referrer = document.querySelector( 'input[name="_wp_http_referer"]' ); |
295
|
|
|
pollux.tabs.tabs = document.querySelectorAll( '.pollux-tabs a' ); |
296
|
|
|
pollux.tabs.views = document.querySelectorAll( '.pollux-config .form-table' ); |
297
|
|
|
|
298
|
|
|
[].forEach.call( pollux.tabs.tabs, function( tab, index ) { |
299
|
|
|
var active = location.hash ? tab.getAttribute( 'href' ).slice(1) === location.hash.slice(2) : index === 0; |
300
|
|
|
if( active ) { |
301
|
|
|
pollux.tabs.setTab( tab ); |
302
|
|
|
} |
303
|
|
|
tab.addEventListener( 'click', pollux.tabs.onClick ); |
304
|
|
|
tab.addEventListener( 'touchend', pollux.tabs.onClick ); |
305
|
|
|
}); |
306
|
|
|
}; |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @return void |
310
|
|
|
*/ |
311
|
|
|
pollux.tabs.onClick = function( ev ) |
312
|
|
|
{ |
313
|
|
|
ev.preventDefault(); |
314
|
|
|
this.blur(); |
315
|
|
|
pollux.tabs.setTab( this ); |
316
|
|
|
location.hash = '!' + this.getAttribute( 'href' ).slice(1); |
317
|
|
|
// pollux.editors.all.forEach( function( editor ) { |
318
|
|
|
// editor.refresh(); |
319
|
|
|
// }); |
320
|
|
|
}; |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return void |
324
|
|
|
*/ |
325
|
|
|
pollux.tabs.setReferrer = function( index ) |
326
|
|
|
{ |
327
|
|
|
var referrerUrl = pollux.tabs.referrer.value.split('#')[0] + '#!' + pollux.tabs.views[index].id; |
328
|
|
|
pollux.tabs.referrer.value = referrerUrl; |
329
|
|
|
}; |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @return void |
333
|
|
|
*/ |
334
|
|
|
pollux.tabs.setTab = function( el ) |
335
|
|
|
{ |
336
|
|
|
[].forEach.call( pollux.tabs.tabs, function( tab, index ) { |
337
|
|
|
var action = pollux.classListAction( tab === el ); |
338
|
|
|
if( action === 'add' ) { |
339
|
|
|
pollux.tabs.active.value = pollux.tabs.views[index].id; |
340
|
|
|
pollux.tabs.setReferrer( index ); |
341
|
|
|
pollux.tabs.setView( index ); |
342
|
|
|
} |
343
|
|
|
tab.classList[action]( 'nav-tab-active' ); |
344
|
|
|
}); |
345
|
|
|
}; |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @return void |
349
|
|
|
*/ |
350
|
|
|
pollux.tabs.setView = function( idx ) |
351
|
|
|
{ |
352
|
|
|
[].forEach.call( pollux.tabs.views, function( view, index ) { |
353
|
|
|
var action = pollux.classListAction( index !== idx ); |
354
|
|
|
view.classList[action]( 'ui-tabs-hide' ); |
355
|
|
|
}); |
356
|
|
|
}; |
357
|
|
|
|
358
|
|
|
jQuery(function() { |
359
|
|
|
for( var key in pollux ) { |
|
|
|
|
360
|
|
|
if( !pollux[key].hasOwnProperty( 'init' ))continue; |
|
|
|
|
361
|
|
|
pollux[key].init(); |
362
|
|
|
} |
363
|
|
|
}); |
364
|
|
|
|
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.